package com.zhuoyue.biz.core.command.cmd.common.scene;

import com.zhuoyue.biz.core.api.domain.LocalSceneDO;
import com.zhuoyue.biz.core.api.dto.req.ControlReq;
import com.zhuoyue.biz.core.command.constant.SceneCmdDeal;
import com.zhuoyue.common.utils.CmdUtils;

import java.util.Map;

/**
 * @author Administrator
 */
public class Scene {
    private static final String FIXED_TIME_FF = "FF";

    public static String control(LocalSceneDO localSceneDO, ControlReq controlReq) {
        String cmd = "";
        switch (controlReq.getControlType()) {
            //场景配置(第一步)
            case SceneCmdDeal.SceneDealType.SCENE_CONFIGURE:
                cmd = getSceneConfigureCmd(localSceneDO, controlReq);
                break;
            //场景配置2(第二步)
            case SceneCmdDeal.SceneDealType.SCENE_LOAD:
                cmd = getSceneLoadCmd(localSceneDO, controlReq);
                break;
            //场景触发
            case SceneCmdDeal.SceneDealType.SCENE_TRIGGER:
                cmd = getSceneTriggerCmd(localSceneDO, controlReq);
                break;
            //场景删除
            case SceneCmdDeal.SceneDealType.SCENE_CLEAR:
                cmd = getSceneClearCmd(localSceneDO, controlReq);
                break;
            default:
                return null;

        }
        return cmd;
    }


    private static String getSceneClearCmd(LocalSceneDO localSceneDO, ControlReq controlReq) {

        String gatewayMac = localSceneDO.getGatewayMac();
        String sceneNumber = localSceneDO.getCode().toString();

        String cmd3To8 = SceneCmdDeal.SceneClear.CMD_3 +
                SceneCmdDeal.SceneClear.CMD_4 +
                gatewayMac +
                SceneCmdDeal.SceneClear.CMD_6 +
                SceneCmdDeal.SceneClear.CMD_7 +
                CmdUtils.intToHexTwo(Integer.parseInt(sceneNumber));

        String cmd2 = CmdUtils.getDateLength(cmd3To8);

        String cmd9 = CmdUtils.getCmdBodySum(cmd3To8);

        String fullCmd = SceneCmdDeal.SceneClear.CMD_1 +
                cmd2 +
                cmd3To8 +
                cmd9 +
                SceneCmdDeal.SceneClear.CMD_10;

        return fullCmd;
    }

    /**
     * 场景配置
     *
     * @param localSceneDO
     * @param controlReq
     * @return
     */
    private static String getSceneConfigureCmd(LocalSceneDO localSceneDO, ControlReq controlReq) {

        String gatewayMac = localSceneDO.getGatewayMac();
        Map<String, Object> actionsMap = controlReq.getActions();

        String sceneNumber = actionsMap.get("sceneNumber").toString();
        String cmdCount = actionsMap.get("cmdCount").toString();
        String fixedHour = actionsMap.get("fixedHour").toString();
        String fixedMinute = actionsMap.get("fixedMinute").toString();

        String cmd3To5 = SceneCmdDeal.SceneConfigure.CMD_3 +
                SceneCmdDeal.SceneConfigure.CMD_4 +
                gatewayMac;

        String cmd10 = CmdUtils.intToHexTwo(Integer.parseInt(sceneNumber));

        String cmd12 = CmdUtils.intToHexTwo(Integer.parseInt(cmdCount));

        String cmd13 = CmdUtils.intToHexTwo(Integer.parseInt(sceneNumber));

        String cmd14 = "";
        if (FIXED_TIME_FF.equals(fixedHour) && FIXED_TIME_FF.equals(fixedMinute)) {
            cmd14 = "FFFF";
        } else {
            cmd14 = CmdUtils.intToHexTwo(Integer.parseInt(fixedHour)) + CmdUtils.intToHexTwo(Integer.parseInt(fixedMinute));
        }

        String cmd7To16 = SceneCmdDeal.SceneConfigure.CMD_7 +
                SceneCmdDeal.SceneConfigure.CMD_8 +
                SceneCmdDeal.SceneConfigure.CMD_9 +
                cmd10 +
                SceneCmdDeal.SceneConfigure.CMD_11 +
                cmd12 +
                cmd13 +
                cmd14 +
                SceneCmdDeal.SceneConfigure.CMD_15 +
                SceneCmdDeal.SceneConfigure.CMD_16;

        String cmd6 = CmdUtils.getDateLength(cmd7To16);

        String cmd3To16 = cmd3To5 +
                cmd6 +
                cmd7To16;

        String cmd2 = CmdUtils.getDateLength(cmd3To16);

        String cmd17 = CmdUtils.getCmdBodySum(cmd3To16);

        String fullCmd = SceneCmdDeal.SceneConfigure.CMD_1 +
                cmd2 +
                cmd3To16 +
                cmd17 +
                SceneCmdDeal.SceneConfigure.CMD_18;

        return fullCmd;
    }


    private static String getSceneLoadCmd(LocalSceneDO localSceneDO, ControlReq controlReq) {

        String gatewayMac = localSceneDO.getGatewayMac();
        Map<String, Object> actionsMap = controlReq.getActions();
        String sceneNumber = actionsMap.get("sceneNumber").toString();
        String cmdCount = actionsMap.get("cmdCount").toString();
        String orderNumber = actionsMap.get("orderNumber").toString();
        String loadCmd = actionsMap.get("loadCmd").toString();
        String timeExpand = actionsMap.get("timeExpand").toString();

        String cmd8 = CmdUtils.intToHexTwo(Integer.parseInt(sceneNumber));

        String cmd9 = CmdUtils.intToHexTwo(Integer.parseInt(cmdCount));

        String cmd10 = CmdUtils.intToHexTwo(Integer.parseInt(orderNumber));

        String cmd7To11 = SceneCmdDeal.SceneLoad.CMD_7 +
                cmd8 +
                cmd9 +
                cmd10 +
                loadCmd;

        String cmd12 = CmdUtils.intToHexTwo(Integer.parseInt(timeExpand));

        String cmd6 = CmdUtils.getDateLength(cmd7To11 + cmd12);

        String cmd3To12 = SceneCmdDeal.SceneLoad.CMD_3 +
                SceneCmdDeal.SceneLoad.CMD_4 +
                gatewayMac +
                cmd6 +
                cmd7To11 +
                cmd12;

        String cmd2 = CmdUtils.getDateLength(cmd3To12);

        String cmd13 = CmdUtils.getCmdBodySum(cmd3To12);

        String fullCmd = SceneCmdDeal.SceneLoad.CMD_1 +
                cmd2 +
                cmd3To12 +
                cmd13 +
                SceneCmdDeal.SceneLoad.CMD_14;

        return fullCmd;
    }


    private static String getSceneTriggerCmd(LocalSceneDO localSceneDO, ControlReq controlReq) {

        String gatewayMac = localSceneDO.getGatewayMac();
        Map<String, Object> actionsMap = controlReq.getActions();
        String sceneNumber = localSceneDO.getCode().toString();

        String cmd8 = CmdUtils.intToHexTwo(Integer.parseInt(sceneNumber));

        String cmd6 = CmdUtils.getDateLength(SceneCmdDeal.SceneTrigger.CMD_7 + cmd8);

        String cmd3To8 = SceneCmdDeal.SceneTrigger.CMD_3 +
                SceneCmdDeal.SceneTrigger.CMD_4 +
                gatewayMac +
                cmd6 +
                SceneCmdDeal.SceneTrigger.CMD_7 +
                cmd8;

        String cmd2 = CmdUtils.getDateLength(cmd3To8);

        String cmd9 = CmdUtils.getCmdBodySum(cmd3To8);

        String fullCmd = SceneCmdDeal.SceneTrigger.CMD_1 +
                cmd2 +
                cmd3To8 +
                cmd9 +
                SceneCmdDeal.SceneTrigger.CMD_10;

        return fullCmd;
    }


}
